package bts.tcc;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLite extends SQLiteOpenHelper {

	public String script;
	
	public SQLite( Context context, 
            String nomeDatabase, 
            CursorFactory factory,
            int version ) {
		super(context, nomeDatabase, factory, version);
	}
	
	@Override
	public void onCreate( SQLiteDatabase db ) {
		db.execSQL( script );
	}

	@Override
	public void onUpgrade( SQLiteDatabase db, 
			               int oldVersion, 
			               int newVersion ) {
		db.execSQL( script );
	}
	
	public Cursor getCursor(String tabela, String[] colunas, String query) {
		try {
			SQLiteDatabase db = getWritableDatabase();
			return db.query( tabela
					, colunas
	         		, query
		     		, null
		     		, null
		     		, null
		     		, null  );
		} catch( SQLException e ) {
			Log.e( "DB_ERRO", 
		       		"Ocorreu o erro [" + e.getMessage() + "]", e );
		}
		
		return null;
	}
	
	public void setScript (String script) {
		this.script = script;
	}
	
	public String getScript () {
		return this.script;
	}
}
